package cn.demomaster.quickui.bamboo;

import android.text.TextUtils;

/**
 * 竹节 流程控制 squirrel桓
 */
public class Bamboo {
    Node rootNode;//根节点
    private Node currentNode;
    public Bamboo() {
    }

    public Bamboo add(Node node){
        if(rootNode==null){
            rootNode = node;
        }else {
            currentNode.setNextNode(node);
        }
        Node lastNode = currentNode;
        currentNode = node;
        currentNode.setLastNode(lastNode);
        return this;
    }

    public void start() {
        if(rootNode!=null){
            rootNode.doJob(rootNode);
        }
    }
    /**
     * 跳转到指定tag的节点
     * @param tag
     */
    public Node findNodeByTag(String tag){
        if(rootNode!=null){
            findNodeByTag(rootNode,tag);
        }
        return rootNode;
    }
    public Node findNodeByTag(Node node, String tag){
        if(node!=null){
            String tag1 = node.getTag();
            if(TextUtils.isEmpty(tag1)&&tag1.equalsIgnoreCase(tag)){
                return node;
            }else {
                return findNodeByTag(node.getNextNode(),tag);
            }
        }
        return rootNode;
    }
    public void destory(){
        if(rootNode!=null){
            releaseNode(rootNode);
        }
    }

    public void releaseNode(Node node){
        if(node!=null){
            Node node1 = node.getNextNode();
            node.setLastNode(null);
            node.setNextNode(null);
            releaseNode(node1);
        }
    }

    public void clear() {
        releaseNode(rootNode);
        if(rootNode!=null) {
            rootNode = null;
        }
    }

    public static abstract class Node implements OnCompleteListener{
        private Node nextNode;
        private Node lastNode;
        private String tag;

        public Node() {
        }
        public Node(String tag) {
            this.tag = tag;
        }

        public void setTag(String tag) {
            this.tag = tag;
        }

        public String getTag() {
            return tag;
        }

        public void setNextNode(Node child) {
            this.nextNode = child;
        }

        public Node getNextNode() {
            return nextNode;
        }

        public void setLastNode(Node lastNode) {
            this.lastNode = lastNode;
        }

        public Node getLastNode() {
            return lastNode;
        }

        /**
         * 工作结束提交结果 执行下一个竹节
         * @param result
         */
        public void submit(Object... result){
            if(nextNode!=null){
                nextNode.doJob(nextNode,result);
            }
        }

        /**
         * 重新执行自身
         * @param result
         */
        public void doSelf(Object... result){
            doJob(this,result);
        }

        /**
         * 回滚到上一层竹节
         * @param result
         */
        public void rollBack(Object... result){
            if(lastNode!=null){
                lastNode.doJob(lastNode,result);
            }
        }
    }

    public interface OnCompleteListener{
        void doJob(Node node,Object... result);
    }
}
